package com.toommi.refuse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toommi.refuse.entity.PageImages;
import com.toommi.refuse.mapper.PageImagesMapper;
import com.toommi.refuse.service.PageImagesService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: IndexImagesServiceImpl
 * @Description: TODO
 * @Auther: HuangBo
 * @Date: 2019/9/12 0012 10:56
 */
@Service
@AllArgsConstructor
public class PageImagesServiceImpl implements PageImagesService {

    private PageImagesMapper pageImagesMapper;

    @Override
    public Object findAllImages(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<Map<String, Object>> allImages = pageImagesMapper.findAllImages();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(allImages);
        Map<String, Object> result = new HashMap<>();
        //把唯一编码
        result.put("count", pageInfo.getTotal());
        result.put("data", allImages);
        result.put("code", 0);
        result.put("msg", "");
        return result;
    }

    @Override
    public List<PageImages> findAllImagesListByImagepage(Integer imgpage) {
        return pageImagesMapper.findAllImagesListByImagepage(imgpage);
    }

    @Override
    public void upload(MultipartFile file, HttpServletRequest request) throws IOException {
        String imgurl = null;
        String type = null;
        String path = null;
        if (file != null) {
            //获取文件原名称
            String originalName = file.getOriginalFilename();
            //文件格式
            type = originalName.substring(originalName.lastIndexOf(".") + 1);
            //项目根路径
            String filePath = request.getServletContext().getRealPath("/");
            //自定义文件名称
            String fileName = "khs_" + String.valueOf(System.currentTimeMillis()) + "." + type;
            //保存图片的路径
            //TODO 需要改成服务器上的路径
            //path = "C:\\Users\\Administrator\\Desktop\\index_img\\" + fileName;
            path = "/opt/khs/pageimg/" + fileName;
            //转存图片到指定路径
            file.transferTo(new File(path));
            //设置图片访问路径
            imgurl = "/refuse-web/pageimg/" + fileName;

            //新增图片
            PageImages image = new PageImages();
            image.setImgurl(imgurl);
            image.setImgnotice("-");
            image.setImghref("-");
            image.setImguse(0);
            image.setImgpage(9);
            pageImagesMapper.addImage(image);
        }
    }

    @Override
    public int addImage(PageImages image) {
        return pageImagesMapper.addImage(image);
    }

    @Override
    public PageImages findOneById(Integer id) {
        return pageImagesMapper.findOneById(id);
    }

    @Override
    public void updateImageById(PageImages image) {
        pageImagesMapper.updateImageById(image);
    }

    @Override
    public void deleteImageById(Integer id) {
        //根据id获取到图片对象
        PageImages image = pageImagesMapper.findOneById(id);
        //获取图片名称
        String imgurl = image.getImgurl();
        String imgName = imgurl.substring(imgurl.lastIndexOf("/") + 1);
        //构造图片路径并删除图片
        //String path = "C:/Users/Administrator/Desktop/index_img/";
        String path = "/opt/khs/pageimg/";
        File file = new File(path + imgName);
        if (file.exists()) {
            file.delete();
        }
        pageImagesMapper.deleteImageById(id);
    }
}
